package com.atguigu.security.security;

import io.jsonwebtoken.CompressionCodec;
import io.jsonwebtoken.CompressionCodecs;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @description: some desc
 * @author: ruoan
 * @date: 2021/2/7 20:30
 */
@Component
public class TokenManager {

    private long tokenExpiration = 24 * 60 * 60 * 1000;
    private String tokenSignkey = "123456";

    // 根据用户名生成token
    public String createToken(String username) {
        String token = Jwts.builder()
                .setSubject(username)
                .setExpiration(new Date(System.currentTimeMillis() + tokenExpiration))
                .signWith(SignatureAlgorithm.HS512, tokenSignkey)
                .compressWith(CompressionCodecs.GZIP)
                .compact();
        return token;
    }


    // 根据token获取用户信息
    public String getUserInfoFromToken(String token) {
        String userinfo = Jwts.parser().setSigningKey(tokenSignkey).parseClaimsJws(token).getBody().getSubject();
        return userinfo;
    }


    // 删除token
    public void removeToken(String token) {
    }
}
